using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using Clue_Less_Shared;
using Clue_Less_Shared.Enumerations;
using System.Xml.Serialization;

namespace Clue_Less_WS
{
	/// <summary>
	/// Main web service
	/// </summary>
	[WebService(Namespace = "http://clue-less.org/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[ToolboxItem(false)]
    [XmlInclude(typeof(CharacterCard)), XmlInclude(typeof(WeaponCard)), XmlInclude(typeof(Card)),
   XmlInclude(typeof(RoomCard)), XmlInclude(typeof(Player)), XmlInclude(typeof(ComboBoxItem))]
	public class MainService : System.Web.Services.WebService
	{

		[WebMethod]
        public ComboBoxItem[] GetOpenGames()
		{
            GamePersistance gp = new GamePersistance();
            return gp.GetOpenGameList();
		}

        [WebMethod]
        public GameStatus GetGameStatus(int gameID)
        {
            GamePersistance gp = new GamePersistance();
            return gp.GetGameStatus(gameID);
        }

        [WebMethod]
        public ComboBoxItem[] GetAvailableCharacters(int gameID)
        {
            GamePersistance gp = new GamePersistance();
            return gp.GetAvailableCharacters(gameID);
        }

        [WebMethod]
        public ComboBoxItem[] GetCharactersWaiting(int gameID)
        {
            GamePersistance gp = new GamePersistance();
            return gp.GetCharactersWaiting(gameID);
        }

        [WebMethod]
        public Character[] GetCharactersPlaying(int gameID, string checksum)
        {
            GamePersistance gp = new GamePersistance();
            Character[] characters = gp.GetCharacters(gameID, true).ToArray();

            if (characters.Length > 0)
            {
                if (gp.GetCharactersChecksum(characters) != checksum)
                    return characters;
            }
            return null;
        }

        [WebMethod]
        public int LoginPlayer(int gameID, Characters character, string name)
        {
            GamePersistance gp = new GamePersistance();
            GameManager gm = new GameManager(gameID, gp);
            int id = gp.LoginPlayer(gameID, character, name);
            if (gp.GetCharactersWaiting(gameID).Length >= gm.HumanPlayerLimit)
            {
                gm.StartGame();
            }
            return id;
        }

        [WebMethod]
        public int CreateGame(int maxPlayers, string name)
        {
            GamePersistance gp = new GamePersistance();
            return gp.CreateGame(name, maxPlayers);
        }

        [WebMethod]
        public bool MakeMove(int gameID, Character character)
        {
            GamePersistance gp = new GamePersistance();
            if (gp.IsMyTurn(gameID, character.PlayerID))
            {
                gp.SaveCharacter(gameID, character);
                return true;
            } else {
                return false;
            }
        }

        [WebMethod]
        public bool ChangeTurn(int gameID)
        {
            GamePersistance gp = new GamePersistance();
            gp.ChangeTurn(gameID);
            return true;
        }

        [WebMethod]
        public bool Accuse(int gameID, int playerID, Rooms room, Characters character, Weapons weapon)
        {
            GamePersistance gp = new GamePersistance();
            GameManager gm = new GameManager(gameID, gp);
            
            return gm.Accuse((Player)gp.GetCharacter(gameID, playerID), weapon, room, character);
        }

        [WebMethod]
        public bool Suggest(int gameID, int playerID, Rooms room, Characters character, Weapons weapon)
        {
            GamePersistance gp = new GamePersistance();
            GameManager gm = new GameManager(gameID, gp);

            return gm.Suggest((Player)gp.GetCharacter(gameID, playerID), weapon, room, character);
        }

        [WebMethod]
        public void Disprove(int gameID, int playerID, int suggestionID, bool skip, Card card)
        {
            GamePersistance gp = new GamePersistance();
            GameManager gm = new GameManager(gameID, gp);

            gm.Disprove((Player)gp.GetCharacter(gameID, playerID), suggestionID, skip, card);
        }


        [WebMethod]
        public void SkipTurn(int gameID, int playerID)
        {
            GamePersistance gp = new GamePersistance();
            if (gp.IsMyTurn(gameID, playerID))
                gp.ChangeTurn(gameID);
        }

        [WebMethod]
        public Envelope GetEnvelope(int gameID)
        {
            GamePersistance gp = new GamePersistance();
            return gp.GetEnvelope(gameID);
        }

        [WebMethod]
        public WeaponPosition[] GetWeapons(int gameID)
        {
            GamePersistance gp = new GamePersistance();
            return gp.GetWeaponPositions(gameID);
        }

        [WebMethod]
        public void QuitGame(int gameID, int playerID)
        {
            GamePersistance gp = new GamePersistance();
            gp.QuitGame(gameID, playerID);
        }

        [WebMethod]
        public Suggestion GetSuggestion(int gameID)
        {
            GamePersistance gp = new GamePersistance();
            return gp.GetSuggestion(gameID);
        }

        [WebMethod]
        public List<Suggestion> GetSuggestions(int gameID)
        {
            GamePersistance gp = new GamePersistance();
            return gp.GetSuggestions(gameID);
        }

        [WebMethod]
        public bool IsMyTurn(int gameID, int playerID)
        {
            GamePersistance gp = new GamePersistance();
            return gp.IsMyTurn(gameID, playerID);
        }

        [WebMethod]
        public Character GetCharacter(int gameID, int playerID)
        {
            GamePersistance gp = new GamePersistance();
            return gp.GetCharacter(gameID, playerID);
        }

        [WebMethod]
        public void SendChatMessage(int gameID, int playerID, string message)
        {
            GamePersistance gp = new GamePersistance();
            gp.SendChatMessage(gameID, playerID, message);
        }

        [WebMethod]
        public ChatMessage[] GetChatMessages(int gameID, string checksum)
        {
            GamePersistance gp = new GamePersistance();
            ChatMessage[] messages = gp.GetChatMessages(gameID);
            
            if (messages.Length > 0)
            {
                if (gp.GetChatChecksum(messages) != checksum)
                    return messages;
            }
            return null;
        }

        [WebMethod]
        public Suggestion test(int gameID)
        {
            GamePersistance gp = new GamePersistance();
            //gp.InsertSuggestion(gameID, 1, Rooms.Ballroom, Weapons.Candlestick, Characters.ColonelMustard);
            Suggestion s = gp.GetSuggestion(gameID);
            gp.SaveSuggestion(gameID,s);
            return s;
        }
	}
}
